package com.example.day07_zhongji;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.day07_zhongji.adapter.VpFragmentAdapter;
import com.example.day07_zhongji.fragment.HomeFragment;
import com.example.day07_zhongji.fragment.WanFragment;
import com.example.day07_zhongji.fragment.WodeFragment;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private Toolbar mToobar;
    private ViewPager mVp;
    private TabLayout mTab;
    private LinearLayout mLinear;
    private NavigationView mNavig;
    private DrawerLayout mDrawer;
    private HomeFragment homeFragment;
    private WodeFragment wodeFragment;
    private WanFragment wanFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        toolbar();
        navig();
        inittab();
    }

    private void inittab() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        homeFragment = new HomeFragment();
        wodeFragment = new WodeFragment();
        wanFragment = new WanFragment();
        fragments.add(homeFragment);
        fragments.add(wodeFragment);
        fragments.add(wanFragment);
        ArrayList<String> strings = new ArrayList<>();
        strings.add("首页");
        strings.add("我的");
        strings.add("项目");
        VpFragmentAdapter vpFragmentAdapter = new VpFragmentAdapter(getSupportFragmentManager(), fragments,strings);
        mVp.setAdapter(vpFragmentAdapter);
        mTab.addTab(mTab.newTab().setText("首页").setIcon(R.drawable.home1));
        mTab.addTab(mTab.newTab().setText("我的").setIcon(R.drawable.cart1));
        mTab.addTab(mTab.newTab().setText("项目").setIcon(R.drawable.mn));
        mTab.setupWithViewPager(mVp);
    }

    private void navig() {
        mNavig.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.music:
                        startActivity(new Intent(MainActivity.this,MediaActivity.class));
                        break;
                    case R.id.me:
                        initme();
                        break;
                }
                return false;
            }
        });
    }

    private void initme() {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
        startActivityForResult(intent,200);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==200){
            Uri data1 = data.getData();
            View headerView = mNavig.getHeaderView(0);
            ImageView viewById = headerView.findViewById(R.id.imageView);
            Glide.with(this).load(data1).into(viewById);
        }
    }

    private void toolbar() {
        mToobar.setTitle("Toolbar");
        setSupportActionBar(mToobar);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawer, mToobar, R.string.app_name, R.string.app_name);
        mDrawer.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        mDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                mLinear.setX(drawerView.getWidth() * slideOffset);
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu1, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.a:
                initpop();
                break;
            case R.id.a1:
                initpop();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initpop() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.pop, null);
        PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.showAsDropDown(mVp,0,0);
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.setOutsideTouchable(true);
        backgroundAlpha(0.5f);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                popupWindow.dismiss();
            }
        });
    }

    private void backgroundAlpha(float v) {
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha=v;
        getWindow().setAttributes(attributes);
    }

    private void initViews() {
        mToobar = findViewById(R.id.toobar);
        mVp = findViewById(R.id.vp);
        mTab = findViewById(R.id.tab);
        mLinear = findViewById(R.id.linear);
        mNavig = findViewById(R.id.navig);
        mDrawer = findViewById(R.id.drawer);
    }
}
